/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.gov.ana.facade;

import br.gov.ana.entities.Orgao;
import br.gov.ana.entities.OrgaoUsuario;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author lucas.nunes
 */
@Stateless
public class OrgaoUsuarioFacade extends AbstractFacade<OrgaoUsuario> {

    @PersistenceContext(unitName = "ResolucaoConjunta032010PU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public OrgaoUsuarioFacade() {
        super(OrgaoUsuario.class);
    }

    public void deleteByOrgao(OrgaoUsuario ou) {
        Query q = em.createQuery("DELETE FROM OrgaoUsuario o WHERE o.useOrgId = :useOrgId");
        q.setParameter("useOrgId", ou.getUseOrgId());
        q.executeUpdate();
        em.flush();
    }

    public OrgaoUsuario findByUseOrgId(Orgao orgao) {
        try {
            Query q = em.createQuery("SELECT o FROM OrgaoUsuario o WHERE o.useOrgId = :useOrgId");
            q.setParameter("useOrgId", orgao);
            q.setFirstResult(0);
            //List<OrgaoUsuario> list = q.getResultList();
            return (OrgaoUsuario) q.getSingleResult();
        } catch (Exception e) {
            return null;
        }

    }

    public List<Orgao> findNotNullSenha() {
        try {
            Query q = em.createQuery("SELECT o FROM OrgaoUsuario o WHERE o.useSe is not null ORDER BY o.useOrgId.orgNm");

            return (List<Orgao>) q.getResultList();
        } catch (Exception e) {
            return null;
        }
    }

    public List<Orgao> findNullSenha() {
        Query q = em.createQuery("SELECT o FROM OrgaoUsuario o WHERE o.useSe is null ORDER BY o.useOrgId.orgNm");

        return (List<Orgao>) q.getResultList();
    }
}
